package airthmetic.exercise.sort.cmp;

import airthmetic.exercise.sort.Sort;

public class BubbleSort2<T extends Comparable<T>> extends Sort<T> {

    @Override
    protected void sort() {
        // 冒泡排序优化   如果末尾已经有序 直接退出循环
        for (int i = array.length - 1; i > 0; i--) {
            boolean sorted = true;
            for (int j = 1; j <= i; j++) {
                if (cmp(j, j - 1) < 0) {
                    swap(j, j - 1);
                    sorted =false;
                }
            }
            if (sorted) break;
        }
    }
}
